package car.aistar.controller;

import car.aistar.dto.BrandAndPrice;
import car.aistar.dto.TqxCarInfo;
import car.aistar.dto.TqxCarSelect;
import car.aistar.entity.CarBrands;
import car.aistar.entity.CarStyle;
import car.aistar.entity.PriceCut;
import car.aistar.service.CarBrandsService;
import car.aistar.service.CarDetailService;
import car.aistar.service.CarStyleService;
import car.aistar.service.TqxCarInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * (CarDetail)表控制层
 *
 * @author makejava
 * @since 2020-09-30 20:15:18
 */
//carDetail/buyCar/carInfo
@Controller
@RequestMapping("/carDetail")
public class CarDetailController {
    /**
     * 服务对象
     */
    @Autowired
    private CarDetailService carDetailService;

    @Autowired
    private TqxCarInfoService tqxCarInfoService;

    @Autowired
    private CarBrandsService carBrandsService;

    @Autowired
    private CarStyleService carStyleService;

    /**
     *本方法用来:买车页面
     *
     *@param
     *@return
     **/
    public  void buyCars(Model model, TqxCarSelect tqxCarSelect){

        /**变速箱*/
        List<String>  cGears = carDetailService.findCgear();
        model.addAttribute("cGears",cGears);

        /**燃油类型*/
        List<String> cCus=carDetailService.findCcus();
        model.addAttribute("cCus",cCus);


        /**车系*/
        CarStyle carStyle =  new CarStyle();

        if(tqxCarSelect.getBrId()!=null){
            carStyle.setBrId(tqxCarSelect.getBrId());
        }

        List<CarStyle> carStyles =carStyleService.queryAll(carStyle);
        model.addAttribute("carStyles",carStyles);


        /**车品牌*/
        List<CarBrands> brands =carBrandsService.findAll();
        model.addAttribute("brands",brands);

        Integer now = tqxCarSelect.getPageNow()==null?1:tqxCarSelect.getPageNow();
        tqxCarSelect.setPageNow(now);
        Integer size = tqxCarSelect.getPageSize()==null?12:tqxCarSelect.getPageSize();
        tqxCarSelect.setPageSize(size);

        tqxCarSelect.setPageNow(null);
        List<TqxCarInfo> carInfoList = tqxCarInfoService.findCarInfo(tqxCarSelect);
        tqxCarSelect.setPageNums(carInfoList.size());

        tqxCarSelect.setPageNow(now);
        carInfoList = tqxCarInfoService.findCarInfo(tqxCarSelect);


        model.addAttribute("carInfoList",carInfoList);

        Map<Long,Double> priceCut = new HashMap<>();
        if(null!=carInfoList){
            for (TqxCarInfo carInfo : carInfoList) {

                List<PriceCut> p = tqxCarInfoService.findPriceCut(carInfo.getcId().intValue());

                if(null!=p && p.size()>0){
                    priceCut.put(carInfo.getcId(),p.get(0).getCutPrice());
                }
            }
        }

        model.addAttribute("priceCut",priceCut);

        PageHelper.startPage(tqxCarSelect.getPageNow(),tqxCarSelect.getPageSize());

        PageInfo<TqxCarInfo> carPageInfo =  new PageInfo<>(carInfoList);
        model.addAttribute("page",carPageInfo);

        System.out.println("buyCars===="+tqxCarSelect);
        model.addAttribute("TqxCarSelect",tqxCarSelect);
    }
    public void indexs(Model model, TqxCarSelect tqxCarSelect){
        Integer now = tqxCarSelect.getPageNow()==null?1:tqxCarSelect.getPageNow();
        tqxCarSelect.setPageNow(now);
        Integer size = tqxCarSelect.getPageSize()==null?12:tqxCarSelect.getPageSize();
        tqxCarSelect.setPageSize(size);

        List<TqxCarInfo> carInfoList = tqxCarInfoService.findNes(tqxCarSelect);
        tqxCarSelect.setPageNow(null);
        List<TqxCarInfo> carInfoLists = tqxCarInfoService.findNes(tqxCarSelect);
        model.addAttribute("carInfoList",carInfoList);
        model.addAttribute("carInfoLists",carInfoLists);
        tqxCarSelect.setPageNow(now);

        /**车品牌*/
        List<CarBrands> brands =carBrandsService.findAll();
        model.addAttribute("brands",brands);

        /**车系*/
        CarStyle carStyle =  new CarStyle();
        List<CarStyle> carStyles =carStyleService.queryAll(carStyle);
        model.addAttribute("carStyles",carStyles);

        List<BrandAndPrice> brandAndPriceList=tqxCarInfoService.findCarPrice();
        model.addAttribute("brandAndPriceList",brandAndPriceList);
//        System.out.println(carInfoList.size());
//        System.out.println(carInfoList.get(0));

        Map<Long,Double> priceCut = new HashMap<>();
        if(null!=carInfoList){
            for (TqxCarInfo carInfo : carInfoList) {

                List<PriceCut> p = tqxCarInfoService.findPriceCut(carInfo.getcId().intValue());
                if(null!=p && p.size()>0){
                    priceCut.put(carInfo.getcId(),p.get(0).getCutPrice());
                }
            }
            System.out.println("priceCut.get(0)===="+priceCut.get(1));
        }

        System.err.println("priceCut.size()"+priceCut.size());
        model.addAttribute("priceCut",priceCut);

        System.out.println("index===="+tqxCarSelect);

    }

    @RequestMapping("buyCar/carInfo")
    public String selectOne(Model model, TqxCarSelect tqxCarSelect) {
        buyCars(model,tqxCarSelect);
        return "buyCar/buyInfo";
    }


    /**
     *本方法用来:
     *卖车跳卖车
     *@param
     *@return
     **/
    @RequestMapping("buyCar/carInfo/{brId}/{csId}/{brName}/{csName}/{price}/{price1}/{price2}/{cSeat}/{orderPrice}/{orderFar}/{orderAge}/{createDate}/{likeStr}/{pageNow}/{pageSize}")
    public String selectOne3(Model model, TqxCarSelect tqxCarSelect) {
        buyCars(model,tqxCarSelect);
        return "buyCar/buyInfo";

    }

    /**
     *本方法用来:首页跳买车
     *
     *@param
     *@return
     **/
    @RequestMapping("buyCar/carInfo4/{brId}/{csId}/{brName}/{csName}/{price}/{price1}/{price2}/{cSeat}/{orderPrice}/{orderFar}/{orderAge}/{createDate}/{likeStr}/{pageNow}/{pageSize}")
    public String selectOne4(Model model, TqxCarSelect tqxCarSelect) {
        buyCars(model,tqxCarSelect);
        model.addAttribute("TqxCarSelect",tqxCarSelect);
        return "buyCar/buyCar02";
    }


    /**
     *@Description //TODO
     *@Date 19:10 2020/10/3
     *@Param
     *@return 首页
     **/



    @RequestMapping("index/carInfo")
    public String indexInfo(Model model, TqxCarSelect tqxCarSelect){
        indexs(model,tqxCarSelect);
        return "index/content";
    }



    @RequestMapping("index/carInfo/{pageNow}/{pageSize}")
    public String indexInfo2(Model model, TqxCarSelect tqxCarSelect){
        indexs(model,tqxCarSelect);
        return "index/content";
    }

}